package cn.pugle.base.thread.highlevelsync;

import java.util.concurrent.Semaphore;

/**
 * @author tzp
 * @since 2019/9/27
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(2);
        new Thread(() -> {
            try {
                semaphore.acquire();
                System.out.println("acquire 1");
                semaphore.acquire();
                System.out.println("acquire 2");
                semaphore.acquire();
                System.out.println("acquire 3");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                Thread.sleep(1000);
                System.out.println("im going to release");
                semaphore.release();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
